<template>
  <div class="components-growth-config gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('user.growthSetting') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="180px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
    >
      <el-form-item
        :label="$t('user.growthSwitch')"
        style="width:440px"
        prop="shopGrowthSwitch"
      >
        <el-switch v-model="dataForm.shopGrowthSwitch" />
      </el-form-item>
      <el-form-item
        v-if="dataForm.shopGrowthSwitch"
        :label="$t('user.perPurchase')"
        style="width:440px"
        prop="buyPrice"
      >
        <el-input
          v-model="dataForm.buyPrice"
          type="number"
          :min="1"
          :max="999999999"
          @change="onSetBuyPrice"
        >
          <template #append>
            {{ $t('coupon.yuan') }}{{ $t('user.getOneGrowth') }}
          </template>
        </el-input>
      </el-form-item>
      <el-form-item
        v-if="dataForm.shopGrowthSwitch"
        :label="$t('user.completedOrderGet')"
        style="width:440px"
        prop="buyOrder"
      >
        <el-input
          v-model="dataForm.buyOrder"
          type="number"
          :min="1"
          :max="999999999"
          @change="onSetBuyOrder"
        >
          <template #append>
            {{ $t('user.growth') }}
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <div
      class="default-btn"
      @click="onSubmit()"
    >
      {{ $t('sysManagement.save') }}
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const dataRule = {
  buyPrice: [
    { required: true, message: $t('user.consumptionGetGrowthCannotEmpty'), trigger: 'blur' }
  ],
  buyOrder: [
    { required: true, message: $t('user.orderAcquisitionCannotEmpty'), trigger: 'blur' }
  ]
}
onMounted(() => {
  onGetData()
})
// 获取数据
const dataForm = ref({
  id: 0,
  buyPrice: 1.0,
  shopGrowthSwitch: false,
  buyOrder: 1.0
})
const dataFormRef = ref(null)
const onGetData = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/user/GrowthConfig/info/' + 'GROWTH_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data.shopGrowthSwitch) {
        dataForm.value = data
      }
    })
    dataFormRef.value.resetFields()
  })
  // }
}
// 提交表单
const onSubmit = () => {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      const param = dataForm.value
      http({
        url: http.adornUrl('/user/GrowthConfig'),
        method: 'post',
        data: http.adornData(param)
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500
        })
      }).catch(() => {

      })
    }
  })
}
const onSetBuyPrice = () => {
  const num = Math.round(dataForm.value.buyPrice)
  dataForm.value.buyPrice = num < 1 ? 1 : num > 999999999 ? 999999999 : num
}
const onSetBuyOrder = () => {
  const num = Math.round(dataForm.value.buyOrder)
  dataForm.value.buyOrder = num < 1 ? 1 : num > 999999999 ? 999999999 : num
}

</script>
<style scoped lang="scss">
.components-growth-config{
  :deep(.el-input-group){
    height: 28px;
  }
}
</style>
